using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.CatalogData;
using WCSFContrib.ECommerceCatalog.User.Services;
using WCSFContrib.ECommerceCatalog.User.Tests.Fakes;
using WCSFContrib.ECommerceCatalog.Product.Interfaces.Services;

namespace WCSFContrib.ECommerceCatalog.User.Tests.Services
{
    /// <summary>
    /// Summary description for CartServiceFixture
    /// </summary>
    [TestClass]
    public class CartServiceFixture
    {
        private CatalogDataSet ds;
        private CartService cartService;

        [TestInitialize()]
        public void MyTestInitialize()
        {
            ds = new CatalogDataSet();

            ds.Category.AddCategoryRow("C1", "General1", 0);
            ds.Category.AddCategoryRow("C2", "General2", 0);
            ds.Category.AddCategoryRow("C1-A", "1-SubGeneral1", 1);
            ds.Category.AddCategoryRow("C1-B", "1-SubGeneral2", 1);
            ds.Category.AddCategoryRow("C2-A", "General3", 2);

            ds.Item.AddItemRow("Item1Description", "item1.png", "Item1", 11, 21, ds.Category[0]);
            ds.Item.AddItemRow("Item2Description", "item2.png", "Item2", 12, 22, ds.Category[1]);
            ds.Item.AddItemRow("Item3Description", "item3.png", "Item3", 13, 23, ds.Category[2]);
            ds.Item.AddItemRow("Item4Description", "item4.png", "Item4", 14, 24, ds.Category[0]);
            ds.Item.AddItemRow("Item5Description", "item5.png", "Item5", 14.99, 25, ds.Category[1]);

            ds.Cart.AddCartRow("jdoe");
            ds.Cart.AddCartRow("hsimpson");
            ds.Cart.AddCartRow("alincoln");

            ds.CartItem.AddCartItemRow(ds.Cart[0], ds.Item[2], 2);
            ds.CartItem.AddCartItemRow(ds.Cart[0], ds.Item[0], 1);
            ds.CartItem.AddCartItemRow(ds.Cart[1], ds.Item[4], 2);
            ds.CartItem.AddCartItemRow(ds.Cart[1], ds.Item[3], 10);
            ds.CartItem.AddCartItemRow(ds.Cart[2], ds.Item[4], 100);

            ds.AcceptChanges();

            cartService = new CartService(ds, new FakeItemsService());
        }

        [TestMethod]
        public void ShouldGetCartById()
        {
            Cart cart = cartService.GetCartByUserName("hsimpson");

            //Assert.AreEqual<string>("hsimpson", cart.User);
            Assert.AreEqual<double>(2, cart.CartItems.Count);
        }

        [TestMethod]
        public void ShouldGetAllCarts()
        {
            ICollection<Cart> allCarts = cartService.AllCarts;
            Assert.IsNotNull(allCarts);
            Assert.AreEqual<int>(3, allCarts.Count);
        }

        [TestMethod]
        public void ShouldAddItem()
        {
            Cart cart;
            cartService.AddItem("jdoe", 3, 10);
            cartService.AddItem("jdoe", 5, 5);

            cart = cartService.GetCartByUserName("jdoe");

            Assert.IsNotNull(cart);
            Assert.AreEqual<int>(3, cart.CartItems.Count);
        }

        [TestMethod]
        public void ShouldRemoveItem()
        {
            Cart cart;
            Category category = new Category(1, "C2", "General2", null);
            Item item = new Item(5, "Item5", "Item5Description", "item5.png", 25, 14.99, category);
            cartService.RemoveItem("hsimpson", item, 1);
            cart = cartService.GetCartByUserName("hsimpson");

            Assert.IsNotNull(cart);
            Assert.AreEqual<int>(2, cart.CartItems.Count);
        }
    }
}
